﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using DevExpress.XtraBars;
using QuanLyKhachSan.DanhMuc;
using System.Threading;
using DevExpress.XtraEditors;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using QuanLyKhachSan.Kho;
namespace QuanLyKhachSan
{
    public partial class Main : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        // Form Đổi mật khẩu
        FrmDoiMatKhau frmDoiMatKhauObj;

        // Form Quản lý Người dùng
        FrmPhanQuyen frmPhanQuyenObj;

        DialogResult result;

        //DataClassesKhachSanDataContext dc;

        public Main()
        {
            InitializeComponent();
        }

        private void barButtonItem21_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void Main_Load(object sender, EventArgs e)
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

            xtraUserControl11.dangNhapThanhCong += new DangNhapEventHandler(HandleLogin);

            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();

            //
           
        }
        // <--- Ham kiem tra mot Form con da duoc mo hay chua
        private Form KiemTraTonTai(Type fType)
        {
            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType() == fType) // Neu Form duoc truyen vao da duoc mo
                {
                    return f;
                }
            }
            return null;
        }

        private void barButtonItem25_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmKhachHang));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmKhachHang frmKH = new frmKhachHang();
                frmKH.MdiParent = this;
                frmKH.Show();
            }
        }

        private void barButtonItem32_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmThucAnThucUong_TTB ));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmThucAnThucUong_TTB frmTA = new frmThucAnThucUong_TTB();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void barButtonItem24_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmSoDoPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmSoDoPhong frmTA = new frmSoDoPhong();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmTinhTrangPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmTinhTrangPhong frmTA = new frmTinhTrangPhong();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void barButtonItem33_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmDonGiaPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmDonGiaPhong frmDG = new frmDonGiaPhong();
                frmDG.MdiParent = this;
                frmDG.Show();
            }
        }

        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmQuanLyDatPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmQuanLyDatPhong frmDP = new frmQuanLyDatPhong();
                frmDP.MdiParent = this;
                frmDP.Show();
            }
        }

        private void barButtonItem26_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmNhanVien));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmNhanVien frmNV = new frmNhanVien();
                frmNV.MdiParent = this;
                frmNV.Show();
            }
        }

        private void barButtonItem29_ItemClick(object sender, ItemClickEventArgs e)
        {
            //xtraUserControl11.Visible = false;

            //Form frm = this.KiemTraTonTai(typeof(frmChucVu ));
            //if (frm != null)
            //{
            //    frm.Activate();
            //}
            //else
            //{
            //    frmChucVu frmNV = new frmChucVu();
            //    frmNV.MdiParent = this;
            //    frmNV.Show();
            //}
        }


        // Hàm xử lý sự kiện đăng nhập thành công ở user control đăng nhập
        private void HandleLogin(object sender, Boolean e)
        {
            XtraMessageBox.Show("Đăng nhập thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DataClassesKhachSanDataContext dc = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);


            if (SystemInfo.pub_QuyenSuDung == "Cán bộ nhập liệu" || SystemInfo.pub_QuyenSuDung.Trim() == "Nhân viên lễ tân")
            {
                PhanQuyenNhapLieu(true);
            }
            else if (SystemInfo.pub_QuyenSuDung.Trim() == "Admin" || SystemInfo.pub_QuyenSuDung.Trim() == "Quản trị hệ thống" )
            {
                PhanQuyenAdmin(true);
            }

        }

        // Phân quyền cho cán bộ nhập liệu
        // Thực hiện các quyền chức năng và báo cáo
        private void PhanQuyenNhapLieu(Boolean duocCap)
        {
            btDoiMatKhau.Enabled = duocCap;
            btDangXuat.Enabled = duocCap;

            btnDanhMucKhachHang.Enabled = duocCap;
        
            btnDanhMucCoPhong.Enabled = duocCap;
            btnDanhMucGiaPhong.Enabled = duocCap;
            btnDanhMucLoaiPhong.Enabled = duocCap;
            btnDanhMucTinhTrangPhong.Enabled = duocCap;
            btnDanhMucKho.Enabled = duocCap;
            btnDanhMucHangHoa.Enabled = duocCap;
            btnDanhMucNganhHang.Enabled = duocCap;
            btnDanhMucDichVu.Enabled = duocCap;

            btnDatPhong.Enabled = duocCap;
            btnThuePhong.Enabled = duocCap;
            btnDanhSachPhong.Enabled = duocCap;
            btbTienPhong.Enabled = duocCap;
            btnNhapKho.Enabled = duocCap;
            btnChuyenKho.Enabled = duocCap;

            btTiGia.Enabled = duocCap;

            btnBaoCaoTonKho.Enabled = duocCap;
            btBaoCaoDanhThu.Enabled = duocCap;
            btBaoCaoBanHang.Enabled = duocCap;
            btBaoCaoCongNo.Enabled = duocCap;
            btBaoCaoThuNo.Enabled = duocCap;

            btNhapDauKy.Enabled = duocCap;

        }

        // Phân quyền cho admin 
        private void PhanQuyenAdmin(Boolean duocCap)
        {
            PhanQuyenNhapLieu(duocCap);

            btTaiKhoan.Enabled = duocCap;

            btSaoLuu.Enabled = duocCap;

            btPhucHoi.Enabled = duocCap;

        }

        private void btDoiMatKhau_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;

            Form frm = this.KiemTraTonTai(typeof(FrmDoiMatKhau));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmDoiMatKhauObj = new FrmDoiMatKhau();
                frmDoiMatKhauObj.Show();
            }
        }

        private void btDangXuat_ItemClick(object sender, ItemClickEventArgs e)
        {
            result = XtraMessageBox.Show("Bạn có muốn đăng xuất khỏi hệ thống?", "Đăng xuất", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                // Form Đổi mật khẩu
                if (frmDoiMatKhauObj != null)
                {
                    frmDoiMatKhauObj.Close();
                }


                // Form Đổi mật khẩu
                if (frmPhanQuyenObj != null)
                {
                    frmPhanQuyenObj.Close();
                }

                xtraUserControl11.Visible = true;

                PhanQuyenAdmin(false);
            }
            else if (result == System.Windows.Forms.DialogResult.Cancel)
            {

            }
        }

        private void btThoat_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btTaiKhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;

            Form frm = this.KiemTraTonTai(typeof(FrmPhanQuyen));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmPhanQuyenObj = new FrmPhanQuyen();
                frmPhanQuyenObj.MdiParent = this;
                frmPhanQuyenObj.Show();
            }
        }

        private void barButtonItem27_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmSuDungDichVu));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmSuDungDichVu frmDV = new frmSuDungDichVu();
                frmDV.MdiParent = this;
                frmDV.Show();
            }
        }

        private void barButtonItem30_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmKho));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmKho frmK = new frmKho();
                frmK.MdiParent = this;
                frmK.Show();
            }
        }

        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmCoPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmCoPhong frmCoPhong = new frmCoPhong();
                frmCoPhong.MdiParent = this;
                frmCoPhong.Show();
            }
        }

        private void btSaoLuu_ItemClick(object sender, ItemClickEventArgs e)
        {
            SaveFileDialog saveFileDialogObj = new SaveFileDialog();
            saveFileDialogObj.Filter = "SQL Server 2005 Back up file|*.bak";
            saveFileDialogObj.Title = "Sao lưu cơ sở dữ liệu";

            try
            {
                if (saveFileDialogObj.ShowDialog() == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;

                    if (File.Exists(saveFileDialogObj.FileName))
                    {
                        File.Delete(saveFileDialogObj.FileName);
                    }

                    SQLServerUtility.SaoLuuCoSoDuLieu(SystemInfo.pub_DatabaseName, saveFileDialogObj.FileName);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void btPhucHoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenFileDialog openFileDialogObj = new OpenFileDialog();
            openFileDialogObj.Filter = "Back up file|*.bak";
            openFileDialogObj.Title = "Phục hồi cơ sở dữ liệu";

            try
            {
                if (openFileDialogObj.ShowDialog() == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    SQLServerUtility.PhucHoiCoSoDuLieu(SystemInfo.pub_DatabaseName, openFileDialogObj.FileName);

                    XtraMessageBox.Show("Chương trình sẽ tự tắt để cập nhật dữ liệu. Xin vui lòng mở lại chương trình sau.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmQuanLyThuePhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmQuanLyThuePhong frmThuePhong = new frmQuanLyThuePhong();
                frmThuePhong.MdiParent = this;
                frmThuePhong.Show();
            }
        }

        private void barButtonItem27_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmSuDungDichVu ));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmDichVu frmDichVu = new frmDichVu();
                frmDichVu.MdiParent = this;
                frmDichVu.Show();
            }
        }

        private void barButtonItem23_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonItem30_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmKho));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmKho frmKhoObj = new frmKho();
                frmKhoObj.MdiParent = this;
                frmKhoObj.Show();
            }
        }

        private void barButtonItem18_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(FrmNganhHang));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                FrmNganhHang frmNganhHangObj = new FrmNganhHang();
                frmNganhHangObj.MdiParent = this;
                frmNganhHangObj.Show();
            }
        }

        private void barButtonItem34_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmLoaiPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmLoaiPhong frmNganhHangObj = new frmLoaiPhong();
                frmNganhHangObj.MdiParent = this;
                frmNganhHangObj.Show();
            }
        }

        private void barButtonItem26_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmThuTienPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmThuTienPhong frmNganhHangObj = new frmThuTienPhong();
                frmNganhHangObj.MdiParent = this;
                frmNganhHangObj.Show();
            }
        }

        private void barButtonItem16_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmThuTienPhong));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmThuTienPhong frmTA = new frmThuTienPhong();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void btnNhapKho_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmNhapKho));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmNhapKho frmTA = new frmNhapKho();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void btTiGia_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmTiGia));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmTiGia frmTA = new frmTiGia();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void btBaoCaoDanhThu_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmBaoCaoDoanhThu));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmBaoCaoDoanhThu frmTA = new frmBaoCaoDoanhThu();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }

        private void btNhapDauKy_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtraUserControl11.Visible = false;
            Form frm = this.KiemTraTonTai(typeof(frmNhapDauKy));
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frmNhapDauKy frmTA = new frmNhapDauKy();
                frmTA.MdiParent = this;
                frmTA.Show();
            }
        }
        // --->
    }
}